package com.zklh.leetcode.interviewTop150;

/**
 * 矩阵置零
 */
public class Solution73 {
    public void setZeroes(int[][] matrix) {
        int maxI = matrix.length;
        int maxJ = matrix[0].length;

        boolean clearTopI = false, cleanTopJ = false;
        for (int i = 0; i < maxI; i++) {
            clearTopI |= matrix[i][0] == 0;
        }
        for (int j = 0; j < maxJ; j++) {
            cleanTopJ |= matrix[0][j] == 0;
        }

        for (int i = 1; i < maxI; i++) {
            for (int j = 1; j < maxJ; j++) {
                if (matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }

        for (int i = 1; i < maxI; i++) {
            if (matrix[i][0] == 0) {
                for (int j = 0; j < maxJ; j++) {
                    matrix[i][j] = 0;
                }
            }
        }

        for (int j = 1; j < maxJ; j++) {
            if (matrix[0][j] == 0) {
                for (int i = 0; i < maxI; i++) {
                    matrix[i][j] = 0;
                }
            }
        }

        if (clearTopI) {
            for (int i = 0; i < maxI; i++) {
                matrix[i][0] = 0;
            }
        }
        if (cleanTopJ) {
            for (int j = 0; j < maxJ; j++) {
                matrix[0][j] = 0;
            }
        }
    }
}
